//
//  StatusPictureView.swift
//  sinaweibo
//
//  Created by icefs2 on 15/11/15.
//  Copyright © 2015年 icefs2. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

private let PictureCellMargin: CGFloat = 5
private let PictureCellId = "PictureCellId"

class StatusPictureView: UICollectionView {
    var imageURLs: [NSURL]? {
        didSet {
            reloadData()
            let viewSize = self.calceViewSize()
            self.snp_updateConstraints { (make) -> Void in
                make.width.equalTo(viewSize.width)
                make.height.equalTo(viewSize.height)
            }
            testLable.text = "\(imageURLs?.count ?? 0)"
        }
    }
    //计算配图试图大小
    private func calceViewSize() -> CGSize {
        let colum: CGFloat = 3.0000000000001
        let maxwidth = screenW - 2 * StatusCellMargin
        //单个图片宽度
        let itemWidth = (maxwidth -  2 * PictureCellMargin) / colum
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        let imagecount = imageURLs?.count ?? 0
        if imagecount  == 0 {
            return CGSizeZero
        }
        //1行
        if imagecount == 1 {
          //  let imageSize = CGSizeMake(180, 120)
            let imageSize = CGSize(width: 180, height: 120)
            layout.itemSize = imageSize
            return imageSize
        }
        //2 行
        if imagecount == 4 {
            let w = 2 * itemWidth + PictureCellMargin
             return CGSize(width: w, height: w)
          //  return CGSizeMake(w, w)
        }
        //多行
        //1 2 3
        let row = CGFloat((imagecount - 1) / 3 + 1)
        let w = maxwidth
        let h = row * itemWidth + (row - 1) * PictureCellMargin
        return CGSize(width: w, height: h)
      //  return CGSizeMake(w, h)
        
    }
    
    //MARK: 3.重写父类的构造方法 调用设置UI
    init() {
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = PictureCellMargin
        layout.minimumLineSpacing = PictureCellMargin
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        registerClass(PictureCell.self, forCellWithReuseIdentifier: PictureCellId)
        dataSource = self
        setupUI()
     }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI() {
        backgroundColor = UIColor.randomColor()
        addSubview(testLable)
        testLable.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.snp_center)
        }
        
    }
    //懒加载测试lable
    private lazy var testLable: UILabel = {
        let l = UILabel()
        l.font = UIFont.systemFontOfSize(25)
        l.textColor = UIColor.redColor()
        l.sizeToFit()
        return l
    }()
    
}
extension StatusPictureView: UICollectionViewDataSource {
   
     func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureCellId, forIndexPath: indexPath) as! PictureCell
        cell.url = imageURLs![indexPath.item]
        //cell.backgroundColor = UIColor.randomColor()
        return cell
        }
}
class PictureCell: UICollectionViewCell {
    var url: NSURL? {
        didSet {
            imageView.sd_setImageWithURL(url, placeholderImage: nil)
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI() {
        contentView.addSubview(imageView)
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
    }
    //MARK: 1. 懒加载所有子控件
    private lazy var imageView: UIImageView = UIImageView()
}
